package com.example.friend.controller;


import com.example.core.constants.Constants;
import com.example.core.controller.BaseController;
import com.example.core.domain.PageDomain;
import com.example.core.domain.R;
import com.example.core.domain.TableDataInfo;
import com.example.core.enums.ResultCode;
import com.example.core.utils.ThreadLocalUtil;
import com.example.friend.annotation.CheckUserStatus;
import com.example.friend.domain.Exam;
import com.example.friend.domain.dto.UserExamDTO;
import com.example.friend.service.IExamService;
import com.example.friend.service.IUserExamService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/user/exam")
@Tag(name = "用户竞赛API")
public class UserExamController extends BaseController {

    @Autowired
    private IUserExamService userExamService;

    @Autowired
    private IExamService examService;

    @Operation(summary = "用户竞赛列表")
    @GetMapping("/list")
    public TableDataInfo list(PageDomain page) {
        return userExamService.list(page);
    }

    @Operation(summary = "报名参赛")
    @PostMapping("/enter")
    @CheckUserStatus
    public R<Void> enterExam(@RequestBody UserExamDTO examSaveDTO) {

        Exam exam = examService.getById(examSaveDTO.getExamId());
        if (exam == null) {
            return R.fail("竞赛不存在", ResultCode.FAILED_NOT_EXISTS);
        }
        if (userExamService.hasEnter(examSaveDTO.getExamId())) {
            return R.fail("用户已经报名", ResultCode.FAILED_ALREADY_EXISTS);
        }
        return toAjax(userExamService.enterExam(examSaveDTO.getExamId()));
    }

    @Operation(summary = "竞赛提交")
    @PostMapping("/submit")
    @CheckUserStatus
    public R<Void> submit(@RequestBody UserExamDTO userExamDTO) {
        return toAjax(examService.submit(userExamDTO.getExamId()));
    }
}
